/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Extras;
import com.sun.istack.logging.Logger;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.ValidatorException;
import javax.mail.MessagingException;
import javax.mail.Part;
import org.primefaces.model.UploadedFile;
/**
 *
 * @author Cristian
 */

/**
 *
 * @author Cristian
 */
@ManagedBean
@SessionScoped
public class FileUploadControl implements Serializable{
  private String destination;
  private UploadedFile file;
    /**
     * Creates a new instance of FileUploadControl
     */
  

    /**
     * @return the file
     */
    public UploadedFile getFile() {
        return file;
    }

       public void setFile(UploadedFile file) {
        this.file = file;
    }
    
      public FileUploadControl() {
         

    }
      
      public void TransferFile(InputStream in){
          try {
              java.text.SimpleDateFormat formato=new java.text.SimpleDateFormat("yyMMddHHmmss");
              String fileName=formato.format(new java.util.Date());
              OutputStream out=new FileOutputStream(new File(destination+fileName+".pdf"));
              int reader=0;
              byte[] bytes=new byte[(int)getFile().getSize()];
              while ((reader=in.read(bytes))!=-1) {
                 out.write(bytes,0,reader);
              }
              in.close();
              out.flush();
              out.close();
              
          } catch (IOException e) {
              System.out.println(e.getMessage());
          }
      }
      
      public void upload(String destino) {
          destination=destino;
    String extValidate;
    if (getFile() != null) {
        String ext = getFile().getFileName();
        if (ext != null) {
            extValidate = ext.substring(ext.indexOf(".")+1);
        } else {
            extValidate = "null";}

            if (extValidate.equals("pdf")||extValidate.equals("PDF")) {
                try {
                    TransferFile(getFile().getInputstream());
                } catch (IOException ex) {
                     // Logger.getLogger(FileUploadControl.class.getName()).log(Level.SEVERE, null, ex);
                    FacesContext context = FacesContext.getCurrentInstance();
                    context.addMessage(null, new FacesMessage("Error", "Error subiendo el archivo..."));
                }
                FacesContext context = FacesContext.getCurrentInstance();
                context.addMessage(null, new FacesMessage("Se ha subido el archivo", getFile().getFileName() + "exitosamente."));
            } else {
                FacesContext context = FacesContext.getCurrentInstance();
                context.addMessage(null, new FacesMessage("Error en la extensiÃ³n", "solamente puede subir archivos .pdf"));
            
            }
        
    } else {
        FacesContext context = FacesContext.getCurrentInstance();
        context.addMessage(null, new FacesMessage("Ha Existido un error", "Select File!"));
    }
}
  
        public boolean uploadvalidate(String file) {
    String extValidate;
    if (file!= null) {
        String ext = file;
        if (ext != null) {
            extValidate = ext.substring(ext.indexOf(".")+1);
             if (extValidate.equals("pdf")||extValidate.equals("PDF")) {
               return true;
            } else {
                return false;
            }
        } else 
        {return false;}
    } else {
       return false;
    }
}
}